package com.ecloud.common.util;

import com.ecloud.common.ResultEnum;
import com.ecloud.common.exception.ServerException;

import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author Squbi
 * @date 2021/7/20 14:42
 */
public class TimeUtil {
    private TimeUtil() {
    }

    public static final String DATE_FORMAT_NORMAL_STANDARD = "yyyy-MM-dd";
    public static final String MONTH_FORMAT_STANDARD = "yyyy-MM";
    public static final String DATE_FORMAT_STANDARD = "yyyyMMdd";
    public static final String DATE_TIME_FORMAT_STANDARD = "yyyyMMddHHmm";

    /**
     * 返回格式化今天的时间
     *
     * @return yyyyMMdd
     * @author Squbi
     * @date 2021/7/20 14:06
     */
    public static String today() {
        return formatDate(new Date());
    }

    public static String now() {
        return formatDateTime(new Date());
    }

    /**
     * 获取昨天的日期
     *
     * @return yyyyMMdd
     * @author Squbi
     * @date 2021/7/20 14:21
     */
    public static String yesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        return formatDate(calendar.getTime());
    }

    public static String formatDate(Date date) {
        return new SimpleDateFormat(DATE_FORMAT_STANDARD).format(date);
    }

    public static String formatDateTime(Date date) {
        return new SimpleDateFormat(DATE_TIME_FORMAT_STANDARD).format(date);
    }

    /**
     * 传入已格式化的字符串 最后一位转为0
     *
     * @param time 已格式化的字符串
     * @return String
     * @author Squbi
     * @date 2021/7/26 17:40
     */
    public static String toWholeTime(String time) {
        byte[] bytes = time.getBytes(StandardCharsets.UTF_8);
        bytes[bytes.length - 1] = '0';
        return new String(bytes, StandardCharsets.UTF_8);
    }

    /**
     * yyyy-MM
     *
     * @return localDate
     * @author Squbi
     * @date 2021/8/2 10:48
     */
    public static LocalDate fromMonthStr(String month) {
        try {
            Date parsed = new SimpleDateFormat(MONTH_FORMAT_STANDARD).parse(month);
            Calendar instance = Calendar.getInstance(Locale.CHINA);
            instance.setTime(parsed);
            return LocalDate.of(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH) + 1, 1);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.ARGS_ERR, e.getMessage());
        }
    }

    public static LocalDate fromDateStr(String date) {
        try {
            Date parsed = new SimpleDateFormat(DATE_FORMAT_NORMAL_STANDARD).parse(date);
            Calendar instance = Calendar.getInstance(Locale.CHINA);
            instance.setTime(parsed);
            return LocalDate.of(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH) + 1, instance.get(Calendar.DAY_OF_MONTH));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.ARGS_ERR, e.getMessage());
        }
    }
}
